// JavaScript Document
var rowid = "row_id";            //tr id
var zoneid = "div_id";		// zone name td id
var stateid = "state_id";		// disable enable span id
var cancel = "";


function Inint_AJAX() 
{
	try 
	{ 
		return new ActiveXObject("Msxml2.XMLHTTP");  
	} 
	catch(e) 
	{} //IE
	try 
	{ 
		return new ActiveXObject("Microsoft.XMLHTTP"); 
	} 
	catch(e) {} //IE
	try 
	{ 
		return new XMLHttpRequest(); 
	} catch(e) {} //Native Javascript
	alert("XMLHttpRequest not supported");
	return null;
};


function edit_div(id) 
{ 
	cancel =  document.getElementById(rowid+"_" + id).innerHTML;
	$(".sub_div_row").hide();
	$("#"+rowid+"_"+id).show();
	var editmode = document.getElementById(rowid+"_" + id);
	editmode.innerHTML = "Loading.....";
	var http = Inint_AJAX();
	http.onreadystatechange = function () 
	{ 	
		if(http.readyState == 4 && http.status == 200) {
		editmode.innerHTML = http.responseText;
		}
	};
	http.open("post", "call_ajax_sub_division_handler.php?edit_id=" + id,true); 
	http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=tis-620"); // set Header
	http.send(null); 
	
}

function default_row(id){
	$(".sub_div_row").show();
	 document.getElementById(rowid+"_" + id).innerHTML = cancel;
}

function save_changes(id,sum_div_name,division_id,circle_id,zone_id){
var d_name  = document.getElementById(sum_div_name);
var d_id = document.getElementById(division_id);
var c_id  = document.getElementById(circle_id);
var z_id  = document.getElementById(zone_id);
var row_change = document.getElementById(rowid+"_"+id);

	if(z_id.value==0){
	$("#circle_msg").html("<font color='red'>zone is not selected</font>");
	z_id.focus();
	return;
	}
	if(c_id.value==0){
	$("#circle_msg").html("<font color='red'>circle is not selected</font>");
	c_id.focus();
	return;
	}
	if(d_id.value == 0){
	$("#circle_msg").html("<font color='red'>Division is not selected</font>");
	d_id.focus();
	return;
	}
	if(d_name.value==0){
	$("#circle_msg").html("<font color='red'>Sub Division name is entered</font>");
	d_name.focus();
	return;
	}



	
	
	row_change.innerHTML = "<td colspan='4' align='center'><b>Saving.....</b></td>";

	var http = Inint_AJAX();
	var edit = {
						"sub_div_id": id,
						"sub_div_name" : d_name.value,
						"div_id": d_id.value,
						"circle_id": c_id.value,
						"zone_id": z_id.value
						}
		var save_change = JSON.stringify(edit);
	http.onreadystatechange = function () 
	{ 	
		if(http.readyState == 4 && http.status == 200) {
		
		row_change.innerHTML = http.responseText;
		$(".sub_div_row").show();
		}
	};
	http.open("post", "call_ajax_sub_division_handler.php?change_subdiv="+ save_change ,true); 
	http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=tis-620"); // set Header
	http.send(null); 

}





function add_division(zone,circle,division,value,msg) {
var sub_name = document.getElementById(value);
var zone_id = document.getElementById(zone);
var circle_id = document.getElementById(circle);
var division_id = document.getElementById(division);


	if(zone_id.value == 0) {
	$("#"+msg).html("<font color='red'>zone is required</font>");
	zone_id.focus();
	return false;
	}
	if(circle_id.value ==0){
	$("#"+msg).html("<font color='red'>Circle is required</font>");
	circle_id.focus();
	return false;
	}
	if(division_id.value ==0){
	$("#"+msg).html("<font color='red'>Division is required</font>");
	division_id.focus();
	return false;
	}
	if(sub_name.value == "") {
		$("#"+msg).html("<font color='red'> Sub Division name is required</font>");
		sub_name.focus();
		return false;
	}
	else {
		var add_new = {
						"sub_div_name" : sub_name.value,
						"div_id": division_id.value,
						"circle_id": circle_id.value,
						"zone_id": zone_id.value
						}
		var json_add = JSON.stringify(add_new);
		
		$("#circle_msg").html("Saving.....");
		
		var http = Inint_AJAX();
		http.onreadystatechange = function () 
		{ 	
			if(http.readyState == 4 && http.status == 200) {
			//alert(http.responseText);
			$("#content_zone:first").prepend(http.responseText);
			$("#circle_msg").html("Sub Division is added successfully");
			$("#add_form").toggle();
			}
		};
		http.open("post", "call_ajax_sub_division_handler.php?obj_add="+json_add,true); 
		http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=tis-620"); // set Header
		http.send(null); 
		
		
		
		
	return false;
	
	}


//	return false;
}

function load_circle(value,select_id) {
	var circle = document.getElementById(select_id);
	circle.innerHTML = "<option value='0'>Loading..</option>";

		var http = Inint_AJAX();
		http.onreadystatechange = function () 
		{ 	
			if(http.readyState == 4 && http.status == 200) {
					
			circle.innerHTML = http.responseText;
			}
		};
		http.open("post", "call_ajax_division_handler.php?load_circle="+value,true); 
		http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=tis-620"); // set Header
		http.send(null); 

}

function load_division(value,select_id){
var division = document.getElementById(select_id);
	division.innerHTML = "<option value='0'>Loading..</option>";

		var http = Inint_AJAX();
		http.onreadystatechange = function () 
		{ 	
			if(http.readyState == 4 && http.status == 200) {
					
			division.innerHTML = http.responseText;
			}
		};
		http.open("post", "call_ajax_sub_division_handler.php?load_division="+value,true); 
		http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=tis-620"); // set Header
		http.send(null); 
}

function status_handler(value,id) {
	var changetag = document.getElementById(stateid+"_"+id);
	changetag.innerHTML ="<font style='font-size: 8.5pt' face='Verdana' >Loading....</font>";
	var http = Inint_AJAX();
	http.onreadystatechange = function () 
	{ 	
		if(http.readyState == 4 && http.status == 200) {
		changetag.innerHTML = http.responseText;
		}
	};
	http.open("post", "call_ajax_sub_division_handler.php?status=" + value + "&&id=" + id,true); 
	http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=tis-620"); // set Header
	http.send(null); 
	

}

function search_row(value){
	if(value ==""){
	$(".sub_div_row").show();
	$("#circle_msg").html("");
	return ;
	}
	
$(".sub_div_row").hide();
$("#circle_msg").html("Loading....");
var http = Inint_AJAX();
	http.onreadystatechange = function () 
	{ 	
		if(http.readyState == 4 && http.status == 200) {
			$("#circle_msg").html("");
		var id_str = JSON.parse(http.responseText);
			if(id_str.length ==0){
			$("#circle_msg").html("No Record Found");
			}
			for(i=0;i< id_str.length;i++) {
				

				
				if(document.getElementById('row_id_' + id_str[i].sub_div_id)==null) {
				//search_new( id_str[i].sub_div_id);
				}
				else
				$("#row_id_"+id_str[i].sub_div_id).show();
				
			}
		}
	};
	http.open("post", "call_ajax_sub_division_handler.php?search=" + value,true); 
	http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=tis-620"); // set Header
	http.send(null); 
	
}


$(document).ready(function(){
						  
						  $("#circle_msg").html("Loading...");
						  $("#content_zone").load("call_ajax_sub_division_handler.php?load=&&start=1",function(){
							$("#circle_msg").html("");	
							});
						  
});

function ajax_content(value,start){
	
	$("#circle_msg").html("Loading");
	
	document.getElementById("content_zone").style.opacity=0.5;
	$(".sub_div_row").hide();
	
	
	//alert(start);
	
	var http = Inint_AJAX();
	http.onreadystatechange = function () 
	{ 	
		if(http.readyState == 4 && http.status == 200) {
			document.getElementById("content_zone").style.opacity=1;
			$("#content_zone").html(http.responseText);
			$("#circle_msg").html("");
			
		
		}
	};
	http.open("post", "call_ajax_sub_division_handler.php?load=" + value+ "&&start="+start,true); 
	http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=tis-620"); // set Header
	http.send(null); 

	
}
